<?php

namespace App\Http\Controllers\Admin;

use App\Facades\Style;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyCertification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CompanyCertificationController extends CommonController{
    /**
     * 审核营业执照
     * @param Request $request
     * @return mixed
     */
    public function comCheck(Request $request){
        $input = $request->only('company_name', 'status');
        $status = trim($request->status);
        $company_name = trim($request->company_name);


        $companyCertification = DB::table('company_certification as t1')
            ->join('company_users as t2', 't1.uid', '=', 't2.uid')
            ->select(
                't2.uid', 't2.viptime',
                't1.company_name', 't1.license', 't1.status','t1.id', 't1.com_id', 't1.updated_at', 't1.remark'
            )->where(function($query) use ($company_name){
                if(strlen($company_name) > 0){
                    $query->where('t1.company_name', 'like', '%' . $company_name . '%');
                }
            })->where(function($query) use ($status){
                if(strlen($status) > 0){
                    $query->where('status', $status);
                }
            })->orderBy('t1.updated_at', 'desc')
            ->paginate(20);


//        $companyCertification = CompanyCertification::
//        where(function($query) use ($company_name){
//            if(strlen($company_name) > 0){
//                $query->where('company_name', 'like', '%' . $company_name . '%');
//            }
//        })->where(function($query) use ($status){
//            if(strlen($status) > 0){
//                $query->where('status', $status);
//            }
//        })->orderBy('updated_at', 'desc')
//            ->paginate(20);


        //分页分配参数 过滤boolean假值
        $appends = arrayFilter($input);
        $curr_url = $request->getRequestUri();//当前URL
        return Style::view('admin.license.comCheck', compact('companyCertification', 'appends', 'status', 'curr_url'));
    }

    /**
     * 通过审核
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function passCheck(Request $request){
        $result = CompanyCertification::where('id', $request->id)->update(['status' => 1]);
        if($result){
            $companyCertification = CompanyCertification::where('id', $request->id)->first(['com_id']);
            CompanyBasic::where('id', $companyCertification->com_id)->update(['cert_status' => 1]);
            flash('通过审核', 'success');
            return redirect()->back();
        }else{
            flash('审核不通过', 'success');
            return redirect()->back();
        }
    }

    /**
     * 批量通过审核
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function passBatchCheck(Request $request){
        $id = $request->input('selected_id.*');
        $result = CompanyCertification::whereIn('id', $id)->update(['status' => 1]);
        if($result){
            $com_id = CompanyCertification::whereIn('id', $id)->pluck('com_id');
            CompanyBasic::whereIn('id', $com_id)->update(['cert_status' => 1]);
        }
        flash('批量审核成功', 'success');
        return redirect()->back();
    }

    /**
     * 不通过审核
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function failCheck(Request $request){
        $result = CompanyCertification::where('id', $request->id)->update(['status' => 2, 'remark' => $request->remark]);
        if($result){
            $companyCertification = CompanyCertification::where('id', $request->id)->first(['com_id']);
            CompanyBasic::where('id', $companyCertification->com_id)->update(['cert_status' => 0]);
            return response()->json(['status' => 0, 'msg' => '审核不通过']);
        }else{
            return response()->json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除审核
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function del(Request $request){
        $companyCertification = CompanyCertification::where('id', $request->id)->first(['com_id', 'license']);
        $result = CompanyCertification::where('id', $request->id)->delete();
        if($result){
            CompanyBasic::where('id', $companyCertification->com_id)->update(['cert_status' => 0]);
            @unlink(public_path() . '/upload/companylicense/' . $companyCertification->license);
            return response()->json(['status' => 0, 'msg' => '删除企业成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除企业失败！']);
        }
    }

    /**
     * 批量删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delBatch(Request $request){
        $id = $request->input('selected_id.*');
        $companyCertification = CompanyCertification::whereIn('id', $id)->select('license', 'com_id')->get();
        CompanyCertification::whereIn('id', $id)->delete();
        $com_id = [];
        foreach($companyCertification as $k => $v){
            $com_id[] = $v->com_id;
            @unlink(public_path() . '/upload/companylicense/' . $v->license);
        }
        CompanyBasic::whereIn('id', $com_id)->update(['cert_status' => 0]);
        flash('批量删除成功', 'success');
        return redirect()->back();
    }
}